package com.shuoyue.ycgk.ui.shop;

import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.util.SmartUtil;
import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.RecruitmentCommentCourse;
import com.shuoyue.ycgk.entity.ShopItem;
import com.shuoyue.ycgk.entity.course_teacher.AppPrice;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.pay.PreBookOrderActivity;
import com.shuoyue.ycgk.ui.recruitment.RecommentCourseAdapter;
import com.shuoyue.ycgk.ui.shop.contract.ShopInfoContract;
import com.shuoyue.ycgk.utils.GlideUtil;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.ResizableImageView;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;
import butterknife.OnClick;

public class ShopInfoActivity extends BaseMvpActivity<ShopInfoContract.Presenter>
        implements ShopInfoContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.img)
    ResizableImageView img;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.free)
    TextView free;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.price_org)
    TextView priceOrg;
    @BindView(R.id.lay_price_org)
    FrameLayout layPriceOrg;
    @BindView(R.id.vip_price)
    TextView vipPrice;
    @BindView(R.id.lay_price_vip)
    LinearLayout layPriceVip;
    @BindView(R.id.lay_price)
    LinearLayout layPrice;
    @BindView(R.id.sold)
    TextView sold;
    @BindView(R.id.go_introduct_line)
    View goIntroductLine;
    @BindView(R.id.lay_introduct)
    LinearLayout layIntroduct;
    @BindView(R.id.go_course_table_line)
    View goCourseTableLine;
    @BindView(R.id.lay_comment)
    LinearLayout layComment;
    @BindView(R.id.lay_tag_title_show)
    LinearLayout layTagTitleShow;
    @BindView(R.id.introduct)
    TextView introduct;
    @BindView(R.id.recomend_recycleView)
    RecyclerView recomendRecycleView;
    @BindView(R.id.lay_recomment)
    LinearLayout layRecomment;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.free2)
    TextView free2;
    @BindView(R.id.price2)
    TextView price2;
    @BindView(R.id.vip_price2)
    TextView vipPrice2;
    @BindView(R.id.lay_price_vip2)
    LinearLayout layPriceVip2;
    @BindView(R.id.lay_price2)
    LinearLayout layPrice2;
    @BindView(R.id.addCar)
    Button addCar;
    @BindView(R.id.buy)
    Button buy;
    @BindView(R.id.go_introduct_line1)
    View goIntroductLine1;
    @BindView(R.id.lay_introduct2)
    LinearLayout layIntroduct2;
    @BindView(R.id.go_course_table_line1)
    View goCourseTableLine1;
    @BindView(R.id.lay_comment2)
    LinearLayout layComment2;
    @BindView(R.id.lay_tag_title)
    LinearLayout layTagTitle;
    private int mScrollY = 0;
    ShopItem goodsInfo;
    int id;

    RecommentCourseAdapter courseAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_info;
    }

    @Override
    protected void initView() {
        pageTitle.setText("详情");
        courseAdapter = new RecommentCourseAdapter(null);
        recomendRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        recomendRecycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        recomendRecycleView.setAdapter(courseAdapter);
        courseAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            RecruitmentCommentCourse commentCourse = (RecruitmentCommentCourse) adapter.getItem(position);
            startActivity(new Intent(mContext, CourseInfoActivity.class)
                    .putExtra("id", commentCourse.getProductId()));
        });
//        registSimpleTextEmptyView(courseAdapter, "暂无推荐课程");

    }

    @Override
    public void initData() {
        id = getIntent().getIntExtra("id", -1);
        mPresenter = new ShopInfoContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getGoodsInfo(id);
    }

    @Override
    public void setGoodsInfo(ShopItem goodsInfo) {
        this.goodsInfo = goodsInfo;
        GlideUtil.loadImage(mContext, goodsInfo.getCover(), img);
        name.setText(goodsInfo.getName());
//        price.setText("￥" + goodsInfo.getDefaultPrice().getPrice());
//        price2.setText("￥" + goodsInfo.getDefaultPrice().getPrice());
//        priceOrg.setText("￥" + goodsInfo.getDefaultPrice().getOriginalPrice());
//        priceOrg2.setText("￥" + goodsInfo.getDefaultPrice().getOriginalPrice());
        sold.setText(Html.fromHtml("已售:" + goodsInfo.getSalesVolume() +
                ((goodsInfo.getRestrictionVolume() - goodsInfo.getSalesVolume()) <= 0 ? "" :
                        "/剩余：<font color='#2196F3'>"
                                + (goodsInfo.getRestrictionVolume() - goodsInfo.getSalesVolume())
                                + "</font>")));
        MyRichTextHelper.showRich(mContext, goodsInfo.getIntroduce(), introduct);

        if (goodsInfo.getRecommendProductDTOS() != null && goodsInfo.getRecommendProductDTOS().size() > 0) {
            courseAdapter.setNewData(goodsInfo.getRecommendProductDTOS());
            scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                private int lastScrollY = 0;
                private final int h = SmartUtil.dp2px(200);

                @Override
                public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if (lastScrollY < h) {
                        scrollY = Math.min(h, scrollY);
                        mScrollY = scrollY > h ? h : scrollY;
                        layTagTitle.setAlpha(1f * mScrollY / h);

                        if (1f * mScrollY / h == 0) {
                            ImmersionBar.with(ShopInfoActivity.this).statusBarDarkFont(false, 0.2f).init();
                        } else if (1f * mScrollY / h == 1) {
                            ImmersionBar.with(ShopInfoActivity.this).statusBarDarkFont(true, 0.2f).init();
                        }
                    }
                    lastScrollY = scrollY;
                    //悬浮标题处理
                    int[] contentLocation = new int[2];
                    layTagTitleShow.getLocationInWindow(contentLocation); //获取在当前窗口内的绝对坐标，含toolBar
                    layTagTitleShow.getLocationOnScreen(contentLocation); //获取在整个屏幕内的绝对坐标，含statusBar
                    int layContentY = contentLocation[1];
                    if (layContentY <= 150) {
                        layTagTitle.setVisibility(View.VISIBLE);
                    } else {
                        layTagTitle.setVisibility(View.GONE);
                    }

                }
            });
        } else {
            layRecomment.setVisibility(View.GONE);
            layTagTitleShow.setVisibility(View.GONE);
            layTagTitle.setVisibility(View.GONE);
        }

        if (goodsInfo.getIsFree() == 1 || goodsInfo.getDefaultPrice() == null) {
            free.setVisibility(View.VISIBLE);
            layPrice.setVisibility(View.GONE);
            free2.setVisibility(View.VISIBLE);
            layPrice2.setVisibility(View.GONE);
            buy.setText("收藏课程");
        } else {
            AppPrice appPrice = goodsInfo.getDefaultPrice();
//            layPriceOrg.setVisibility(appPrice.getPrice() == appPrice.getOriginalPrice() ? View.GONE : View.VISIBLE);
//            layPriceOrg2.setVisibility(appPrice.getPrice() == appPrice.getOriginalPrice() ? View.GONE : View.VISIBLE);
//            layPriceVip.setVisibility(appPrice.getPrice() == appPrice.getVipPrice() ? View.GONE : View.VISIBLE);
//            layPriceVip2.setVisibility(appPrice.getPrice() == appPrice.getVipPrice() ? View.GONE : View.VISIBLE);
            layPriceOrg.setVisibility(TextUtils.isEmpty(appPrice.getOriginalPrice()) ? View.GONE : View.VISIBLE);
//            layPriceOrg2.setVisibility(0 == appPrice.getOriginalPrice() ? View.GONE : View.VISIBLE);
            layPriceVip.setVisibility(TextUtils.isEmpty(appPrice.getVipPrice()) ? View.GONE : View.VISIBLE);
            layPriceVip2.setVisibility(TextUtils.isEmpty(appPrice.getVipPrice()) ? View.GONE : View.VISIBLE);
            price.setText("￥" + appPrice.getPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
            price2.setText("￥" + appPrice.getPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
            priceOrg.setText("￥" + appPrice.getOriginalPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
//            priceOrg2.setText("￥" + (int) appPrice.getOriginalPrice());
            vipPrice.setText("￥" + appPrice.getVipPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
            vipPrice2.setText("￥" + appPrice.getVipPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())


        }
    }

    @Override
    public void addShoppingcar() {
        toast("加入成功");
        addCar.setVisibility(View.INVISIBLE);
    }


    @OnClick({R.id.back,
            R.id.service,
            R.id.shopping_car,
            R.id.share,
            R.id.addCar,
            R.id.buy,
            R.id.lay_introduct,
            R.id.lay_comment,
            R.id.lay_introduct2,
            R.id.lay_comment2}
    )
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.service:
//                CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");
//                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constant.serviceUrl)));
                if (Constant.isOpenServiceExploer) {
                    Uri uri = Uri.parse(Constant.serviceUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } else {
                    CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");

                }

                break;
            case R.id.shopping_car:
                startActivity(new Intent(this, ShoppingCarActivity.class));
                break;
            case R.id.share:
                if (goodsInfo != null) {
                    ShareUtil.shareUrl(this, ShareUtil.BOOK_URL + goodsInfo.getId(),
                            goodsInfo.getName(),
                            "",
                            null);
                }
//                ShareUtil.shareUrl(this, Constant.shareGift, null);
                break;
            case R.id.addCar:
                mPresenter.addShoppingcar(id);
                break;
            case R.id.buy:
                if (goodsInfo != null && goodsInfo.getDefaultPrice() != null)
                    PreBookOrderActivity.start(mContext, goodsInfo, goodsInfo.getDefaultPrice().getPrice(),
                            goodsInfo.getDefaultPrice().getId(), goodsInfo.getSubjectTypeId());
                break;
            case R.id.lay_comment:
            case R.id.lay_comment2:
                goCourseTableLine.setVisibility(View.VISIBLE);
                goIntroductLine.setVisibility(View.INVISIBLE);
                goCourseTableLine1.setVisibility(View.VISIBLE);
                goIntroductLine1.setVisibility(View.INVISIBLE);
                float y1 = layRecomment.getY();
                scrollView.scrollTo(0, (int) y1 - (SizeUtil.dip2px(mContext, 82)/**顶部title高度加边距**/) - layTagTitle.getHeight());
                break;
            case R.id.lay_introduct:
            case R.id.lay_introduct2:
                goCourseTableLine.setVisibility(View.INVISIBLE);
                goIntroductLine.setVisibility(View.VISIBLE);
                goCourseTableLine1.setVisibility(View.INVISIBLE);
                goIntroductLine1.setVisibility(View.VISIBLE);
                float y2 = introduct.getY();
                scrollView.scrollTo(0, (int) y2 - (SizeUtil.dip2px(mContext, 82)/**顶部title高度加边距**/) - layTagTitle.getHeight());

                break;
        }
    }

    @Override
    protected void onDestroy() {
        RichText.clear(this);
        super.onDestroy();
    }


}
